package com.activities.api.data.providers;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.activities.api.data.entities.Activity;

public class ActivityStoreImpl extends StoreImpl<Activity> implements ActivityStore {

	@Override
	public Activity get(String id) {
		EntityManager manager = getManager();
		Activity result;
		try {
			result = manager.find(Activity.class, id);
		} finally {
			manager.close();
		}

		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Activity> getAll() {
		EntityManager manager = getManager();
		try {
			Query query = manager.createQuery(String.format("SELECT x FROM %s x", Activity.class.getName()));
			Collection<Activity> result = query.getResultList();
			result.size();
			
			return result;
		} finally {
			manager.close();
		}

	}

	@Override
	public void save(Activity item) {
		EntityManager manager = getManager();
		try {
			manager.persist(item);
		} finally {
			manager.close();
		}
	}

	@Override
	public void delete(String id) {
		EntityManager manager = getManager();

		try {
			Activity item = manager.find(Activity.class, id);

			manager.remove(item);
		} finally {
			manager.close();
		}
	}

}
